package com.czkt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czkt.entity.CstService;
import com.czkt.entity.SalPlan;
import com.czkt.entity.vo.PlanAndChance;
import com.czkt.service.ISalChanceService;
import com.czkt.service.ISalPlanService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
public class SalPlanController {

    @Resource
    ISalPlanService salPlanService;

    @Resource
    ISalChanceService salChanceService;

    @RequestMapping("/plan/list")
    public String selectServiceShow(@RequestParam(value = "chcCustName",defaultValue = "") String chcCustName,
                                    @RequestParam(value = "chcLinkman",defaultValue = "") String chcLinkman,
                                    @RequestParam(value = "chcTitle",defaultValue = "") String chcTitle,
                                    @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                    @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize, Model model){
        IPage<PlanAndChance> serviceIPage = salPlanService.selectBatchIds(chcCustName, chcLinkman, chcTitle, pageIndex, pageSize);
        model.addAttribute("planPage",serviceIPage);
        model.addAttribute("chcCustName",chcCustName);
        model.addAttribute("chcLinkman",chcLinkman);
        model.addAttribute("chcTitle",chcTitle);
        return "/plan/list";
    }

    @RequestMapping("/plan/add")
    public String addService(Model model){
        model.addAttribute("chances",salChanceService.list());
        return "/plan/add";
    }

    @RequestMapping("/plan/insert")
    public String insertService(SalPlan salPlan){
        if (salPlanService.save(salPlan)){
            return "redirect:/plan/list";
        }else {
            return "/plan/add";
        }

    }

    @RequestMapping("/plan/edit")
    public String updateService(String chcId,Model model){
        model.addAttribute("chances",salChanceService.list());
        model.addAttribute("salPlans",salPlanService.selectAllByPlaChcId(chcId));
        return "/plan/edit";
    }

    @RequestMapping("/plan/update")
    public String toUpdateService(SalPlan salPlan){
        System.out.println(salPlan.getPlaResult());
        salPlanService.updateById(salPlan);
        return "redirect:/plan/list";
    }

    @RequestMapping("/plan/del/{chcId}")
    @ResponseBody
    public Object deleteService(@PathVariable("chcId") Integer chcId){
        return salPlanService.removeById(chcId);
    }

}
